<template>
  <div class="leave-detail">

    <div class="preview-box1">
      <span>姓名：{{recordInfo.userName}}</span>
      <span>身份证号码：{{recordInfo.userCardNumber}}</span>
      <span>银行卡号：{{recordInfo.bankNumber}}</span>
      <span>银行名称：{{recordInfo.bankName}}</span>
    </div>

    <div class="preview-box1">
      <span>企业名称：{{recordInfo.mechanismName}}</span>
      <span>离职日期：{{recordInfo.workEndTime}}</span>
      <span>离职方式：{{workEndType}}</span>
      <span>员工来源：{{userOrigin}}</span>
    </div>

    <div class="preview-box2">
      <span>本月工时1及工价1：{{recordInfo.currentWorkHours}}小时&nbsp;&nbsp;{{recordInfo.currentWorkPrice}}元/每小时</span>
      <span v-if="recordInfo.currentWorkHours2 >0 && recordInfo.currentWorkPrice2>0">本月工时2及工价2：{{recordInfo.currentWorkHours2}}小时&nbsp;&nbsp;{{recordInfo.currentWorkPrice2}}元/每小时</span>
      <div class="pre-box" v-if="recordInfo.lastWorkHours && recordInfo.lastWorkPrice">
        <span>上月工时1及工价1：{{recordInfo.lastWorkHours}}小时&nbsp;&nbsp;{{recordInfo.lastWorkPrice}}元/每小时</span>
      </div>
      <div class="pre-box" v-if="recordInfo.lastWorkHours2>0 && recordInfo.lastWorkPrice2>0">
        <span>上月工时2及工价2：{{recordInfo.lastWorkHours2}}小时&nbsp;&nbsp;{{recordInfo.lastWorkPrice2}}元/每小时</span>
      </div>
      <span class="total-money">应发工资：{{totalMoney}}元</span>

      <span class="lend-money" v-if="recordInfo.onlineLendMoney">蓝聘线上借支：-{{recordInfo.onlineLendMoney}}元</span>

      <div class="deduct-subsidy">
        <span v-if="deductList.length" v-for="item in deductList">{{item.withhold}}：-{{item.withholdMoney}}元</span>
        <span v-if="subsidyList.length" v-for="item in subsidyList">{{item.subsidy}}：+{{item.subsidyMoney}}元</span>
      </div>

      <span class="money">实发工资：{{gainMoney}}元</span>
    </div>

    <div class="preview-box3">
      <span>人力来源：{{recordInfo.userSource}}</span>
      <span>发放方式：{{recordInfo.provideType == 1?'转员工':'转代理'}}</span>
      <div class="proxy-box" v-if="recordInfo.provideType == 2">
        <span>代理姓名：{{recordInfo.agencyName}}</span>
        <span>代理身份证号：{{recordInfo.agencyCardNumber}}</span>
        <span>代理银行卡号：{{recordInfo.agencyBankNum}}</span>
        <span>代理银行名称：{{recordInfo.agencyBankName}}</span>
        <span>代理银行卡归属地：{{recordInfo.agencyBankAddress}}</span>
      </div>
    </div>

    <div class="btn-box" v-if="showBtn == 1">
      <button type="button" class="save" @click="modifyLeave">修改</button>
    </div>

    <div class="btn-box" v-if="showBtn == 2">
      <button type="button" class="btn cancel" @click="backPage">返回修改</button>
      <button type="button" class="btn confirm" @click="leaveBill">提交</button>
    </div>

    <backpage @backPage="goBack" v-if="showBtn == 1"></backpage>

  </div>
</template>

<script>
  import {baseUrl} from "../../../config/env"
  import backpage from "../../../components/backpage"

  export default {
    data() {
      return {
        id: "",
        userOrigin:"",//员工来源
        workEndType: "",//离职方式
        deductContent: "",//扣款项目
        subsidyContent: "",//补贴项目
        totalMoney: "",//应发工资
        gainMoney: "",//实际工资
        deductList: [],
        subsidyList: [],
        userSign: "",
        recordInfo: "",
        lendMoney: "",//借支
        showBtn: this.$route.query.showBtn,
        withHoldMoney: 0,
        subsidyMoney: 0,

        jobSubsidy: "",//岗位津贴
        nightSubsidy: "",//夜班津贴
        hotelSubsidy: "",//住宿补助
        monthAward: "",//月度绩效奖
        eatSubsidy: "",//伙食补助
        attendanceBonus: "",//全勤奖
        lastMonthSubsidy: "",//上月补发
        otherSubsidy: "",//其他补贴

        compensate: "",//赔偿
        eatFee: "",//伙食费
        waterPowerFee: "",//水电网络费
        premium: "",//保险
        overdueFee: "",//合同未到期扣款
        absenteeismFee: "",//旷工扣款
        clothFee: "",//工衣鞋扣款
        borrowLendMoney: "",//线下借支
        withholdLendMoney: "",//代扣借支
        lastMonthBalance: "",//上月负数
        incomeTax:"",//个人所得税
        otherFee: "",//其他扣款
      }
    },
    mounted() {
      this.id = this.$route.query.id;
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));
      this.queryDetail();
    },
    methods: {
      changeEmpOrigin(value){
        if ("DH" == value) {
          this.userOrigin = "大和";
        } else if ("LP" == value) {
          this.userOrigin = "蓝聘";
        } else if ("SS" == value) {
          this.userOrigin = "上势";
        } else if ("DYH" == value) {
          this.userOrigin = "大赢和";
        }
      },
      selectOrigin(value) {//员工来源
        if ("大和" == String(value)) {
          return "DH";
        } else if ("蓝聘" == String(value)) {
          return "LP";
        } else if ("上势" == String(value)) {
          return "SS";
        } else {
          return "DYH";
        }
      },
      changeWorkEndType(value){
        if ("1" == value) {
          this.workEndType = "合同到期";
        } else if ("2" == value) {
          this.workEndType = "辞退、开除";
        } else if ("3" == value) {
          this.workEndType = "辞工";
        }
      },
      queryDetail() {
        this.$vux.loading.show({
          text: '加载中',
        });

        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/get_resign_record_list?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.get(baseUrl + "/resident/get_resign_record_list?sgin=" + this.userSign + "&userCardNumber=" + "&id=" + this.id + "&day=" + "&auth=" + signature + "&timestamp=" + time).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0 && resultObj.data != null) {
            this.recordInfo = resultObj.data[0];
            this.changeWorkEndType(resultObj.data[0].workEndType);

            if(sessionStorage.getItem("userOrigin")){
              this.userOrigin = sessionStorage.getItem("userOrigin");
            }else{
              this.changeEmpOrigin(resultObj.data[0].userOrigin);
            }

            this.totalMoney = parseFloat(Number(resultObj.data[0].currentWorkHours) * Number(resultObj.data[0].currentWorkPrice) + Number(resultObj.data[0].currentWorkHours2) * Number(resultObj.data[0].currentWorkPrice2) + Number(resultObj.data[0].lastWorkHours) * Number(resultObj.data[0].lastWorkPrice) + Number(resultObj.data[0].lastWorkHours2) * Number(resultObj.data[0].lastWorkPrice2)).toFixed(2);//应发工资
            this.lendMoney = resultObj.data[0].onlineLendMoney;

            if (resultObj.data[0].status == 2) {//离职单状态 1为财务审核中2财务已处理完成
              this.showBtn = 0;
            } else {
              this.showBtn = this.$route.query.showBtn ? this.$route.query.showBtn : 1;
            }

            //处理新增之后扣款记录
            if (sessionStorage.getItem("withholdList")) {
              this.deductList = JSON.parse(sessionStorage.getItem("withholdList"));

              let temp = "";
              if (this.deductList != null && this.deductList.length > 0) {
                for (let i = 0; i < this.deductList.length; i++) {
                  this.withHoldMoney += Number(this.deductList[i].withholdMoney);
                  temp += this.deductList[i].withhold + "-" + this.deductList[i].withholdMoney + ";";//住宿费-300;社保-50

                  this.changeWithholdText(this.deductList[i].withhold, this.deductList[i].withholdMoney);
                }
                this.deductContent = temp;
              }
            } else {
              this.deductContent = resultObj.data[0].deductContent;
              if (this.deductContent) {
                let deductItem = this.deductContent.split(";");
                let arrayDeduct = [];
                for (let i = 0; i < deductItem.length; i++) {
                  if (deductItem[i]) {
                    this.withHoldMoney = Number(deductItem[i].split("-")[1]) + Number(this.withHoldMoney);
                    let obj = {};
                    obj.withhold = deductItem[i].split("-")[0];
                    obj.withholdMoney = Number(deductItem[i].split("-")[1]);
                    obj.mark = 1;
                    arrayDeduct.push(obj);
                  }
                }
                sessionStorage.setItem("withholdList", JSON.stringify(arrayDeduct));
                this.deductList = arrayDeduct;
              }
            }

            //处理新增之后补贴
            if (sessionStorage.getItem("subsidyList")) {
              this.subsidyList = JSON.parse(sessionStorage.getItem("subsidyList"));
              let short = "";
              if (this.subsidyList != null && this.subsidyList.length > 0) {
                for (let i = 0; i < this.subsidyList.length; i++) {
                  this.subsidyMoney += Number(this.subsidyList[i].subsidyMoney);
                  short += this.subsidyList[i].subsidy + "-" + this.subsidyList[i].subsidyMoney + ";";//住宿费-300;社保-50

                  this.changeSubsidyText(this.subsidyList[i].subsidy, this.subsidyList[i].subsidyMoney);
                }
                this.subsidyContent = short;
              }
            } else {
              this.subsidyContent = resultObj.data[0].subsidyContent;
              if (this.subsidyContent) {
                let subsidyItem = this.subsidyContent.split(";");
                let arraySubsidy = [];
                for (let i = 0; i < subsidyItem.length; i++) {
                  if (subsidyItem[i]) {
                    this.subsidyMoney = Number(subsidyItem[i].split("-")[1]) + Number(this.subsidyMoney);
                    let obj = {};
                    obj.subsidy = subsidyItem[i].split("-")[0];
                    obj.subsidyMoney = Number(subsidyItem[i].split("-")[1]);
                    obj.mark = 1;
                    arraySubsidy.push(obj);

                  }
                }
                sessionStorage.setItem("subsidyList", JSON.stringify(arraySubsidy));
                this.subsidyList = arraySubsidy;
              }
            }

            this.gainMoney = (Number(this.totalMoney) - Number(this.lendMoney) - Number(this.withHoldMoney) + Number(this.subsidyMoney)).toFixed(2);//实发工资
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      changeWithholdText(text, money) {
        if (text == "赔偿") {//赔偿
          this.compensate = money;
        } else if (text == "伙食费") {//伙食费
          this.eatFee = money;
        } else if (text == "水电网络费") {//水电网络费
          this.waterPowerFee = money;
        } else if (text == "保险") {//保险
          this.premium = money;
        } else if (text == "合同未到期扣款") {//合同未到期扣款
          this.overdueFee = money;
        } else if (text == "旷工扣款") {//旷工扣款
          this.absenteeismFee = money;
        } else if (text == "工衣鞋扣款") {//工衣鞋扣款
          this.clothFee = money;
        } else if (text == "线下借支") {//线下借支
          this.borrowLendMoney = money;
        } else if (text == "代扣借支") {//代扣借支
          this.withholdLendMoney = money;
        } else if (text == "上月负数") {//上月负数
          this.lastMonthBalance = money;
        } else if(text == "个人所得税"){//个人所得税
          this.incomeTax = money;
        } else if (text == "其他扣款") {//其他扣款
          this.otherFee = money;
        }
      },
      changeSubsidyText(text, money) {
        if (text == "岗位津贴") {//岗位津贴
          this.jobSubsidy = money;
        } else if (text == "夜班津贴") {//夜班津贴
          this.nightSubsidy = money;
        } else if (text == "住宿补助") {//住宿补助
          this.hotelSubsidy = money;
        } else if (text == "月度绩效奖") {//月度绩效奖
          this.monthAward = money;
        } else if (text == "伙食补助") {//伙食补助
          this.eatSubsidy = money;
        } else if (text == "全勤奖") {//全勤奖
          this.attendanceBonus = money;
        } else if (text == "上月补发") {//上月补发
          this.lastMonthSubsidy = money;
        } else if (text == "其他补贴") {//其他补贴
          this.otherSubsidy = money;
        }
      },
      modifyLeave() {//修改
        this.$router.push({
          path: "/leavehistorymodify",
          query: {
            "leaveId": this.id,
            "deductContent": this.deductContent,
            "subsidyContent": this.subsidyContent,
            "lendMoney": this.lendMoney,
            "userOrigin": this.userOrigin
          }
        });
      },
      backPage() {//返回修改
        this.$router.push({
          path: "/leavehistorymodify",
          query: {
            "leaveId": this.id,
            "deductContent": this.deductContent,
            "subsidyContent": this.subsidyContent,
            "lendMoney": this.lendMoney,
            "userOrigin": this.userOrigin
          }
        });
      },
      leaveBill() {//提交
        this.$vux.loading.show({
          text: '加载中',
        });

        let data = {
          "id": this.id,
          "userOrigin": this.selectOrigin(this.userOrigin),
          "deductContent": this.deductContent, //扣款项目
          "subsidyContent": this.subsidyContent, //补贴项目

          "jobSubsidy": this.jobSubsidy,//岗位津贴
          "nightSubsidy": this.nightSubsidy,//夜班津贴
          "hotelSubsidy": this.hotelSubsidy,//夜班津贴
          "monthAward": this.monthAward,//月度绩效奖
          "eatSubsidy": this.eatSubsidy,//伙食补助
          "attendanceBonus": this.attendanceBonus,//全勤奖
          "lastMonthSubsidy": this.lastMonthSubsidy,//上月补发
          "otherSubsidy": this.otherSubsidy,//其他补贴

          "compensate": this.compensate,//赔偿
          "eatFee": this.eatFee,//伙食费
          "waterPowerFee": this.waterPowerFee,//水电网络费
          "premium": this.premium,//保险
          "overdueFee": this.overdueFee,//合同未到期扣款
          "absenteeismFee": this.absenteeismFee,//旷工扣款
          "clothFee": this.clothFee,//工衣鞋扣款
          "borrowLendMoney": this.borrowLendMoney,//线下借支
          "withholdLendMoney": this.withholdLendMoney,//代扣借支
          "lastMonthBalance": this.lastMonthBalance,//上月负数
          "incomeTax": this.incomeTax,//个人所得税
          "otherFee": this.otherFee//其他扣款
        };

        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/get_resign_record_list?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.post(baseUrl + "/resident/get_resign_record_list?sgin=" + this.userSign + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 0) {
              this.$vux.toast.text("离职单修改失败，请重试", "middle");
            } else {
              sessionStorage.removeItem("withholdList");
              sessionStorage.removeItem("subsidyList");
              sessionStorage.removeItem("subsidy_withhold");
              sessionStorage.removeItem("userOrigin");
              this.$router.push({path: "/leavehistory"});
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      goBack() {
        sessionStorage.removeItem("withholdList");
        sessionStorage.removeItem("subsidyList");
        sessionStorage.removeItem("subsidy_withhold");
        sessionStorage.removeItem("userOrigin");
        this.$router.push({path: "/leavehistory"});
      }
    },
    components: {
      backpage
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"
  .leave-detail
    wh(100%, 100%)
    background: rgba(242, 242, 242, 1)
    .preview-box1
      display: flex
      flex-direction: column
      justify-content: center
      margin-bottom: 0.267rem
      wh(100%, auto)
      background: rgba(255, 255, 255, 1)
      span
        margin-left: 0.32rem
        padding-top: 0.2rem
        padding-bottom: 0.2rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          size-color(0.453rem, rgba(51, 51, 51, 1))
          font-weight: 500
    .preview-box2
      display: flex
      flex-direction: column
      justify-content: center
      margin-bottom: 0.267rem
      wh(100%, auto)
      background: #FFF
      span
        margin-left: 0.32rem
        padding-top: 0.2rem
        padding-bottom: 0.2rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          padding-top: 0.467rem
      .pre-box
        display: flex
        flex-direction: column
        span
          margin-left: 0.32rem
          padding-top: 0.2rem
          padding-bottom: 0.2rem
          size-color(0.427rem, rgba(153, 153, 153, 1))
      .deduct-subsidy
        display: flex
        flex-direction: column
        span
          margin-left: 0.32rem
          padding-top: 0.2rem
          padding-bottom: 0.2rem
          size-color(0.427rem, rgba(153, 153, 153, 1))
          &:first-child
            padding-top: 0.467rem
      .total-money
        font-weight: 500
        color: rgba(102, 102, 102, 1)
      .lend-money
        padding-top: 0.64rem
      .money
        padding-bottom: 0.48rem
        size-color(0.453rem, rgba(60, 175, 255, 1))
        font-weight: 500
    .preview-box3
      display: flex
      flex-direction: column
      justify-content: center
      wh(100%, auto)
      background: rgba(255, 255, 255, 1)
      span
        margin-left: 0.32rem
        padding-top: 0.2rem
        padding-bottom: 0.2rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          padding-top: 0.48rem
        &:last-child
          padding-bottom: 0.747rem
      .proxy-box
        display: flex
        flex-direction: column
    .btn-box
      display: flex
      justify-content: center
      align-items: center
      padding-top: 0.96rem
      padding-bottom: 0.64rem
      width: 100%
      height: 1.28rem
      background: rgba(242, 242, 242, 1)
      .save
        width: 92.53%
        height: 1.28rem
        line-height: 1.28rem
        font-size: 0.453rem
        border-radius: 0.16rem
        color: rgba(255, 255, 255, 1)
        background: #FF5353
      .btn
        width: 4.267rem
        height: 1.28rem
        line-height: 1.28rem
        font-size: 0.453rem
        border-radius: 0.16rem
        color: rgba(255, 255, 255, 1)
      .cancel
        margin-right: 0.227rem
        background: rgba(255, 83, 83, 1)
      .confirm
        margin-left: 0.227rem
        background: rgba(60, 175, 255, 1)
</style>
